package com.panshi.controller.article;

import com.panshi.api.ArticleApi;
import com.panshi.api.SortApi;
import com.panshi.po.ArticlePo;
import com.panshi.po.SortPO;
import com.panshi.vo.ArticleAlterVo;
import com.panshi.vo.ArticleDeleteVo;
import com.panshi.vo.ArticleUpdateVo;
import com.panshi.vo.UpdateLeaveVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 文章修改
 */
@Controller
@Slf4j
public class UpdateArticleController {

    @Autowired
    private ArticleApi articleApi;


    @Resource
    private SortApi sortApi;


    /**
     * 根据文章id获取文章信息
     * @param id
     * @param model
     * @param request
     * @return
     */
    @GetMapping("updateArticle")
    public String update(@RequestParam Integer id, Model model,HttpServletRequest request){

        List<SortPO> listes=  sortApi.totalQuery().getData();

        ArticlePo articlePo=articleApi.idQuery(id).getData();
        model.addAttribute("articlePo",articlePo);
        request.setAttribute("listes",listes);
        return "article/article-edit";
    }

    /**
     * 修改文章
     * @param articleAlterVo
     * @return
     */
    @PostMapping("article-updateArticle")
    @ResponseBody
    public Map<String,Object> updateArticle(ArticleAlterVo articleAlterVo){

        log.info("articleAlterVo========={}",articleAlterVo);
        Map<String,Object> map = new HashMap<>();


      Integer integer=  articleApi.articleAlter(articleAlterVo).getData();

        if(integer == 0){
            map.put("success",false);
            map.put("message","修改失败，请重新修改");
        }else if(integer == 1){
            map.put("success",true);
            map.put("message","修改成功");
        }
        return map;
    }

    /**
     * 删除文章
     * @param articleDeleteVo
     * @return
     */
    @PostMapping("article-deleteArticle")
    @ResponseBody
    public  Map<String,Object> deleteArticle(ArticleDeleteVo articleDeleteVo){

        Map<String,Object> map = new HashMap<>();
        Integer integer=articleApi.articleDelete(articleDeleteVo).getData();
        log.info("integer============{}",integer);
        if(integer == 1){
            map.put("success",true);
            map.put("message","删除成功");
        }else{
            map.put("success",false);
            map.put("message","删除失败，请重新删除");
        }

        return map;
    }

}
